#include "stdafx.h"
#include "SampleAppForWin32.h"

#define MAX_LOADSTRING 100

HINSTANCE hInst;
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];

//////////////////////////////////////////////////////////////////////////
// Start of TRUEMapLib
//////////////////////////////////////////////////////////////////////////
#define ID_BUTTON_BASE							0x1234
#define ID_BUTTON_ZOOM_IN						(ID_BUTTON_BASE + 0)
#define ID_BUTTON_ZOOM_OUT						(ID_BUTTON_BASE + 1)
#define ID_BUTTON_GOTO_TRUEMOBILE				(ID_BUTTON_BASE + 2)

TMMapHandle g_hTMMap;
HWND g_hButtonZoomIn;
HWND g_hButtonZoomOut;
HWND g_hButtonGoToTRUEMobile;
//////////////////////////////////////////////////////////////////////////
// End of TRUEMapLib
//////////////////////////////////////////////////////////////////////////

ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	MSG msg;
	HACCEL hAccelTable;

	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_SAMPLEAPPFORWIN32, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SAMPLEAPPFORWIN32));

	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SAMPLEAPPFORWIN32));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_SAMPLEAPPFORWIN32);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//////////////////////////////////////////////////////////////////////////
// Start of TRUEMapLib
//////////////////////////////////////////////////////////////////////////
TMVoid __stdcall OnMapEvent(TMMapHandle hMap, TMEventType eventType, LPARAM lParam, WPARAM wParam, TMVoid *pUserParam)
{
	int nUserParam;

	// The pUserParam argument is 3rd argument of tmRegisterEventCallback() in InitInstance().
	// In this sample, the argument value is like this:
	// tmRegisterEventCallback(g_hTMMap, OnMapEvent, (TMVoid *)10);
	// So pUserParam is to be '(TMVoid *)10'.
	nUserParam = (int)pUserParam;

	// You may use nUserParam in here.

	// Print eventType to output window to test event callback.
	switch(eventType)
	{
	case TMEventTypeMapWillLoad:
		OutputDebugString(_T("TMEventTypeMapWillLoad\n"));
		break;

	case TMEventTypeMapLoaded:
		OutputDebugString(_T("TMEventTypeMapLoaded\n"));
		break;

	case TMEventTypeMapWillDraw:
		OutputDebugString(_T("TMEventTypeMapWillDraw\n"));
		break;

	case TMEventTypeMapDrawn:
		OutputDebugString(_T("TMEventTypeMapDrawn\n"));
		break;
	}
}
//////////////////////////////////////////////////////////////////////////
// End of TRUEMapLib
//////////////////////////////////////////////////////////////////////////

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance;

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   //////////////////////////////////////////////////////////////////////////
   // Start of TRUEMapLib
   //////////////////////////////////////////////////////////////////////////
   RECT rcWindow;
   RECT rcTMMap;
   int nWindowWidth;
   int nWindowHeight;
   TMLocation location;

   // start location
   location.dLatitude = 37.433446;
   location.dLongitude = 127.173493;

   GetClientRect(hWnd, &rcWindow);

   nWindowWidth = rcWindow.right - rcWindow.left;
   nWindowHeight = rcWindow.bottom - rcWindow.top;

   SetRect(&rcTMMap, 0, 0, nWindowWidth, nWindowHeight - 70);

   // Initialize TRUE.MapLib
   if(tmInitLibrary() == TRUE)
   {
	   // It creates the TRUEMapLib object.
	   g_hTMMap = tmCreate(WS_CHILD | WS_BORDER | WS_VISIBLE, rcTMMap, hWnd, hInstance);

	   // It registers callback function to get map events.
	   tmRegisterEventCallback(g_hTMMap, OnMapEvent, (TMVoid *)10);

	   if(g_hTMMap != NULL)
	   {
		   // It loads and shows the map from Google(TM) Maps server.
		   tmLoad(g_hTMMap, TMMapTypeGoogle, location, 17);
	   }
	   else
	   {
		   // An error occurred while creating TRUEMapLib object.
		   MessageBox(hWnd, _T("Could not create TRUEMapLib object"), _T("TRUE.Map"), MB_OK);
	   }

	   // Create buttons.
	   g_hButtonZoomIn = CreateWindow(_T("button"), _T("Zoom In"), WS_CHILD | WS_VISIBLE, 0, rcTMMap.bottom, nWindowWidth / 3, 30, hWnd, (HMENU)ID_BUTTON_ZOOM_IN, hInstance, NULL);
	   g_hButtonZoomOut = CreateWindow(_T("button"), _T("Zoom Out"), WS_CHILD | WS_VISIBLE, nWindowWidth / 3, rcTMMap.bottom, nWindowWidth / 3, 30, hWnd, (HMENU)ID_BUTTON_ZOOM_OUT, hInstance, NULL);
	   g_hButtonGoToTRUEMobile = CreateWindow(_T("button"), _T("TRUEMobile"), WS_CHILD | WS_VISIBLE, nWindowWidth / 3 * 2, rcTMMap.bottom, nWindowWidth / 3, 30, hWnd, (HMENU)ID_BUTTON_GOTO_TRUEMOBILE, hInstance, NULL);
   }
   else
   {
	   // An error occurred while initializing TRUEMapLib.
	   MessageBox(hWnd, _T("Could not initialize TRUEMapLib"), _T("TRUE.Map"), MB_OK);
   }
   //////////////////////////////////////////////////////////////////////////
   // End of TRUEMapLib
   //////////////////////////////////////////////////////////////////////////

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;

		//////////////////////////////////////////////////////////////////////////
		// Start of TRUEMapLib
		//////////////////////////////////////////////////////////////////////////
		case ID_BUTTON_ZOOM_IN:
			// If the zoom-in button is pressed.
			tmZoomIn(g_hTMMap);
			break;

		case ID_BUTTON_ZOOM_OUT:
			// If the zoom-out button is pressed.
			tmZoomOut(g_hTMMap);
			break;

		case ID_BUTTON_GOTO_TRUEMOBILE:
			{
				TMLocation location;

				// Set the default location
				location.dLatitude = 37.433446;
				location.dLongitude = 127.173493;

				// Calls the tmSetMapCenterLocation() API to change latitude/longitude
				tmSetMapCenterLocation(g_hTMMap, location);

				// Calls the tmSetZoomLevel() API to change zoom level
				tmSetZoomLevel(g_hTMMap, 6);
			}

			break;
		//////////////////////////////////////////////////////////////////////////
		// End of TRUEMapLib
		//////////////////////////////////////////////////////////////////////////

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		//////////////////////////////////////////////////////////////////////////
		// Start of TRUEMapLib
		//////////////////////////////////////////////////////////////////////////
		// Destroy map control.
		tmDestroy(g_hTMMap);

		// Uninitialize TRUE.MapLib
		tmUninitLibrary();

		// Destroy two buttons.
		DestroyWindow(g_hButtonZoomIn);
		DestroyWindow(g_hButtonZoomOut);
		//////////////////////////////////////////////////////////////////////////
		// End of TRUEMapLib
		//////////////////////////////////////////////////////////////////////////

		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
